// 可以直接在主题入口导入 Vue 文件
// VitePress 已预先配置 @vitejs/plugin-vue
import type { Theme } from 'vitepress'
import { isClient } from '@vueuse/core'
import ElementPlus, {
  ID_INJECTION_KEY,
  ZINDEX_INJECTION_KEY,
} from 'element-plus'
import { globals } from '~/components'
import Layout from './Layout.vue'

import '@unocss/reset/tailwind.css'
import 'element-plus/dist/index.css'
import 'virtual:uno.css'
import '~/styles/css-vars.scss'
import 'nprogress/nprogress.css'

export default {
  Layout,
  enhanceApp: async ({ app, router }) => {
    app.use(ElementPlus)
    app.provide(ID_INJECTION_KEY, { prefix: 1024, current: 0 })
    app.provide(ZINDEX_INJECTION_KEY, { current: 0 })
    globals.forEach(([name, Comp]) => {
      app.component(name, Comp)
    })
    if (!isClient)
      return

    const nprogress = await import('nprogress')
    router.onBeforeRouteChange = (to) => {
      if (to === '/') {
        setTimeout(() => {
          router.go('/home/index.html')
        }, 30)
        return
      }
      nprogress.start()
    }

    router.onAfterPageLoad = () => {
      nprogress.done()
    }
  },
} satisfies Theme
